<?php
require_once 'Database/Database2.php';
/**
 * Diese Klasse stellt Methoden für die Gallerie Administration zur Verfügung
 *
 * @author Andy
 */
class GallerieManager {
    private $database; 
    private $EVENTTABLE = "GAL_EVENT";
    private $PICTABLE = "GAL_PICS";
    private $DESTPATH = "../../gallerie/auto_gal_2011";
    private $SRCPATH = "../../gallerie/upload";
    private $GALPATH = "../../gallerie";
    
    public function GallerieManager(){
        $this->database = new Database();               
    }
    
    
    public function UploadPictures(){
        // Verbindung zur Datenbank herstellen
        if($_REQUEST['bestehend']){
            // Angaben zur bestehenden Tabelle herauslesen
            $sql ='select * from '.$this->EVENTTABLE.' where EVENT_KEY = '.$_REQUEST['b_gallerie'];
            $resultArray = $this->database->query($sql);	
            $pfad = $resultArray[0]['EVENT_PATH'];
            $event_key = $resultArray[0]['EVENT_KEY'];
            $event_bez = $resultArray[0]['EVENT_BEZ'];
	}
	else{
            // Neuen Eintrag in Eventtabelle machen
            $sql = 'INSERT INTO '.$this->EVENTTABLE.' (`EVENT_KEY`, `EVENT_BEZ`, `EVENT_DATE`, `EVENT_PATH`) 
		VALUES (NULL, "'.$_REQUEST['n_gallerie'].'","'.	$_REQUEST['date'].'","auto_gal_2011")';            
            
            $this->database->iquery($sql);
            $sql = 'SELECT max(`EVENT_KEY`) as max FROM '.$this->EVENTTABLE;
            $resultArray = $this->database->query($sql); 
            $event_key = $resultArray[0]['max'];
            $event_bez = $_REQUEST['n_gallerie'];
        }
	
	$sql = 'SELECT max(`PICS_KEY`) as max FROM '.$this->PICTABLE;
	$resultArray = $this->database->query($sql);	
	$flnr_pic = $resultArray[0]['max'];

        $i =1;
	foreach ($_SESSION['pic'] as $key => $wert) {				            
            $bild = $this->SRCPATH."/".$wert;            
            if(file_exists($bild)){
                $imgsize = getimagesize($bild);
		$bild_new = strtoupper(substr($event_bez,0,3))."_".date("Ymd",mktime())."_".str_pad( $flnr_pic, 10, '0', STR_PAD_LEFT );
		echo $bild_new,'.jpg';
		
                $im = ImageCreateFromJPEG($bild);
		$width = $imgsize[0];
		$height = $imgsize[1];
                
                if ($imgsize[0]>$imgsize[1]){
                    // Querformat		
                    $anztwidth = 350*$width/$height;
                    $anztheight = 350;
                    $pretwidth = 184;
                    $pretheight = 184*$height/$width;
                }
                else{
                    // Hochformat
                    $anztheight = 500;                        
                    $anztwidth = 500*$width/$height;
                    $pretwidth = 184*$width/$height;
                    $pretheight = 184;
                }
                $im2 = ImageCreateTruecolor($anztwidth,$anztheight);
		ImageCopyResized($im2,$im,0,0,0,0,$anztwidth,$anztheight,$width,$height);
		ImageJpeg($im2, $this->DESTPATH."/ANZ/".$bild_new.".jpg",90);		
                
		echo ' Bild erstellt... ';
                $im2 = ImageCreateTruecolor($pretwidth,$pretheight);
		ImageCopyResized($im2,$im,0,0,0,0,$pretwidth,$pretheight,$width,$height);
                ImageJpeg($im2, $this->DESTPATH."/PRE/".$bild_new.".jpg",90);
									
		echo ' Vorschaubild erstellt... ';                            
                $sql = 'INSERT INTO `GAL_PICS` (`PICS_KEY`, `PICS_EVENT`, `PICS_PRE`, `PICS_ANZ`) 
                    VALUES (NULL, "'.$event_key.'", "'.$bild_new.'.jpg", "'.$bild_new.'.jpg")';
            
                $this->database->iquery($sql);
                echo ' Eintrag in DB...';
                unlink("../../gallerie/upload/".$wert);
                echo ' Original gelöscht... <br />';
            }
            else{
                echo "Das Bild ",$wert," ist nicht auf dem Server vorhanden.";
            }
            $i++;
            $flnr_pic++;
        } 
        unset($_SESSION['pic']);
    }
    
    /**
     * Gibt das nächste zuzuteilende Bild zurück
     * @return type 
     */
    public function GetNextUnallocatePicID(){
        $sql = "SELECT PICS_KEY 
            FROM GAL_PICS 
            WHERE GAL_PICS.PICS_MPIC is null 
            LIMIT 0,1";
        $resultArray = $this->database->query($sql);
        return $resultArray[0][0];                
    }
    
    public function GetImagePathByID($id){
        $sql  = "SELECT EVENT_PATH, PICS_ANZ FROM GAL_EVENT, GAL_PICS WHERE GAL_PICS.PICS_KEY = $id AND GAL_PICS.PICS_EVENT = GAL_EVENT.EVENT_KEY LIMIT 0,1";
        $resultArray = $this->database->query($sql);
        return $this->GALPATH."/".$resultArray[0]['EVENT_PATH']."/ANZ/".$resultArray[0]['PICS_ANZ'];
    }
}

?>
